package com.duo.result;

import com.duo.enums.errorcode.BaseCode;
import com.duo.enums.errorcode.StatusCode;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import java.io.Serializable;

/**
 * @Author: Xrx
 * @Description: 返回格式基础类
 * @CreateTime: 2023/5/14 10:13
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class BaseResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private int code;
    private String message;
    private T data;

    public BaseResult(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public BaseResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public BaseResult(StatusCode code, T data) {
        this.code = code.getCode();
        this.message = code.getMessage();
        this.data = data;
    }

    public BaseResult(StatusCode code) {
        this(code, null);
    }

    public BaseResult(T data) {
        this.code = BaseCode.OK.getCode();
        this.message = BaseCode.OK.getMessage();
        this.data = data;
    }

    /**
     * 失败返回函数
     */
    public static BaseResult error(StatusCode statusCode) {
        return new BaseResult<>(statusCode);
    }

    /**
     * 失败返回函数
     */
    public static BaseResult error(String message) {
        return new BaseResult<>(message);
    }

    /**
     * 成功返回函数 (无数据)
     */
    public static BaseResult success() {
        BaseCode baseCode = BaseCode.OK;
        return new BaseResult<>(baseCode.getCode(), baseCode.getMessage());
    }

    /**
     * 成功返回函数 (带数据)
     */
    public static <T> BaseResult<T> success(String message) {
        BaseCode baseCode = BaseCode.OK;
        return new BaseResult<>(baseCode.getCode(), message, null);
    }

    /**
     * 成功返回函数 (带数据)
     */
    public static <T> BaseResult<T> success(String message, T data) {
        return new BaseResult<>(BaseCode.OK.getCode(), message, data);
    }

}

